package com.xiaoshangkou.hellopra.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.bean.Result;
import com.xiaoshangkou.hellopra.mapper.MemberMapper;
import com.xiaoshangkou.hellopra.mapper.PostMapper;
import com.xiaoshangkou.hellopra.mapper.UserGroupMapper;
import com.xiaoshangkou.hellopra.pojo.Member;
import com.xiaoshangkou.hellopra.pojo.Post;
import com.xiaoshangkou.hellopra.pojo.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 *
 * 关于数据库中的用户服务层
 *
 */
@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;


    @Autowired
    private BCryptPasswordEncoder encoder;//密码加密

    //分页查询用户
    public Page<Member> findPage(int page,int size){
        Page memberPage = memberMapper.selectPage(new Page<>(page, size), null);
        return memberPage;
    }

    //启用/禁用用户
    public void updateStatus(Integer mid){
        Member member = memberMapper.selectById(mid);
        member.setBlackHome(!member.isBlackHome());//取反
        memberMapper.updateById(member);

    }

    //根据名称查询用户(模糊查询)
    public Page<Member> findPageByName(int page,int size,String name){
        //构建一个查询的wrapper
        QueryWrapper<Member> wrapper = new QueryWrapper();

        wrapper.like(StringUtils.isNotBlank(name),"username",name);
        Page memberPage = memberMapper.selectPage(new Page<>(page, size), wrapper);
        return memberPage;
    }

    //查找用户所拥有的用户组
    public Member findMember(Integer mid){
        return memberMapper.selectById(mid);
    }

    //查询所有用户组
    public List<UserGroup> findAll(){
        return userGroupMapper.selectList(null);
    }

    //给用户重新赋用户组
    public void updateUserGroup(Integer ids,Integer mid){
        Member member = memberMapper.selectById(mid);
        member.setUid(ids);
        memberMapper.updateById(member);
    }


    //注册
    public Result register(Member member){
        //1保存用户
        //验证用户名是否重复
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",member.getUsername());
        List<Member> members = memberMapper.selectList(queryWrapper);
        if (members.size()>0){
            return new Result(false,"用户名已存在");
        }
        //验证邮箱是否重复
        QueryWrapper<Member> queryWrapper2= new QueryWrapper<>();
        queryWrapper2.eq("email",member.getEmail());
        List<Member> members2 = memberMapper.selectList(queryWrapper2);
        if (members2.size()>0){
            return new Result(false,"邮箱已存在");
        }
        //加密密码
        String password = member.getPassword();
        password=encoder.encode(password);
        member.setPassword(password);
        //设置用户状态为true
        member.setActive(true);

        //保存用户
        memberMapper.insert(member);
        return new Result(true,"注册成功!");
    }

    //登录
    public Result login(String name,String password){
        Member member=null;

        //根据用户名查询
        if (member==null){
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",name);
            member=memberMapper.selectOne(queryWrapper);
        }

        //根据邮箱查询
        if (member==null){
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("email",name);
            member=memberMapper.selectOne(queryWrapper);
        }

        //没有查询到用户
        if (member==null){
            return new Result(false,"用户名或密码错误");
        }

        //验证密码
        boolean flag = encoder.matches(password, member.getPassword());
        if (!flag){
            return new Result(false,"用户名或密码错误");
        }

        //验证是否被拉黑
        if(member.isBlackHome()){
            return new Result(false,"该用户已被拉黑！！无法登录");
        }


        //存member用于验证用户是否登录
        return new Result(true,"登录成功",member);

    }

    //查询用户信息
    public Member findByMid(Integer mid){
        return memberMapper.selectById(mid);
    }

    //根据用户id查询用户帖子
    public List<Post> findPostByMid(Integer mid){
        return postMapper.findPostByMid(mid);
    }

    //修改用户
    public void update(Member member, HttpSession session){


        //旧密码
        String oldPassword = memberMapper.selectById(member.getMid()).getPassword();
        //新密码
        String newPassword = member.getPassword();
        //如果新密码不等于旧密码，对新密码进行加密
        if (!oldPassword.equals(newPassword)){
            member.setPassword(encoder.encode(member.getPassword()));
        }
        memberMapper.updateById(member);
        //重新刷新session
        Member newMember = memberMapper.selectById(member.getMid());
        //将用户信息存入session
        session.setAttribute("member",newMember);

    }


}
